
package com.glela.order.service.impl.order.amqp;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.glela.cache.redis.RedisKeys;
import com.glela.cache.redis.RedisUtil;
import com.glela.common.exception.PlatformException;
import com.glela.order.model.request.RequestOrderDetail;
import com.glela.order.model.response.RespAddOrderInfo;
import com.glela.order.service.OrderService;
import com.glela.order.service.impl.order.OrderServiceImpl5;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.response.ResponseResultModel;

/**
 * 下单监听队列
 * @author zls
 *
 */
public class AmqpReceiverOrder {
	private static final int _CACH_SECONDES = 60 * 15 * 1;
	//	private static final int	_CACH_PROCESSING_SECONDES	= 60 * 60 * 1;

	private static Logger logger = LoggerFactory.getLogger(OrderServiceImpl5.class);

	private OrderService	orderService;
	private RedisUtil		redisUtil;

	public void onMessage(RequestOrderDetail requestOrderDetail) {
		long currentTimeMillis = System.currentTimeMillis();
		String parentOrderSn = null;
		ResponseResultModel<RespAddOrderInfo> addOrderInfo = null;
		String clientType = null;//渠道类型
		try {
			if (requestOrderDetail == null) {
				logger.error("下单消息为空");
				return;
			} else {
				parentOrderSn = requestOrderDetail.getParentOrderSn();
				if (requestOrderDetail.getReqHeader() != null) {
					clientType = requestOrderDetail.getReqHeader().getPhoneType();
				}
			}
			if (StringUtils.isBlank(parentOrderSn)) {
				return;
			}
			try {
				addOrderInfo = orderService.addOrderInfo(requestOrderDetail);
			} catch (PlatformException e) {
				addOrderInfo = new ResponseResultModel<RespAddOrderInfo>(e.getErrorEnum(), new RespAddOrderInfo());
				addOrderInfo.setMessage(e.getMessage());
			}
		} catch (Exception e) {
			logger.error("下单处理队列错误：处理消息失败：{}-{}", requestOrderDetail, e.getMessage(), e);
			addOrderInfo = new ResponseResultModel<RespAddOrderInfo>(ErrorEnum.C20000, new RespAddOrderInfo());
			addOrderInfo.setMessage("大家太热情啦，门都被挤破啦！");
		} finally {//消息反馈
			if (StringUtils.isNotEmpty(parentOrderSn)) {//有消息
				logger.warn("下单处理队列处理订单：{}", parentOrderSn);
				try {
					//缓存下单结果
					if (addOrderInfo != null) {
						redisUtil.setex(RedisKeys.ORDER_AMQP_ADD_RESULT + parentOrderSn, _CACH_SECONDES, JSON.toJSONString(addOrderInfo));
					}
					//移除redis排队订单
					redisUtil.srem(RedisKeys.ORDER_QUEUES_ADD, parentOrderSn);
				} catch (Exception e) {
					logger.error("下单处理队列错误：移除redis订单队列失败：{}-{}", parentOrderSn, JSON.toJSONString(requestOrderDetail));
				}
			} else {
				logger.error("下单消息内容异常：{}-{}", parentOrderSn, JSON.toJSONString(requestOrderDetail));
			}
			if (logger.isInfoEnabled()) {
				logger.warn("异步下单处理耗时：{}:{}-{}ms", parentOrderSn, clientType, System.currentTimeMillis() - currentTimeMillis);
			}
			clientType = null;
			requestOrderDetail = null;
			parentOrderSn = null;
		}
	}

	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

	public void setRedisUtil(RedisUtil redisUtil) {
		this.redisUtil = redisUtil;
	}
}